﻿#include <QPainter>
#include <QDebug>
#include <QColor>
#include "math.h"
#include "draw.h"
#include "dbutil.h"
#include <QtSql>
#include <string>
#include <QString>
#include <Windows.h>
#include <iostream>
#include <QPaintDevice>
using namespace std;

Draw::Draw(QQuickPaintedItem *parent)
    :QQuickPaintedItem(parent){}

void Draw::setYear(int year){
    syear=year;
    //qDebug()<<syear;
}

void Draw::modeSelect(int mode){
    m_mode=mode+1;
}

void Draw::setMonth(int month){
    smonth=month;
}

void Draw::setDay(int day){
    sday=day;
}

void Draw::setHour(int hour){
    //qDebug()<<hour;
    shour=hour;
}

void Draw::setMinute(int minute){
    sminute=minute;
}

void Draw::setTimeRange(int range){
    TimeRange=range;
}

void Draw::setStartTime(int m){
    if(m==4) StartTime=to_string(syear)+"-"+to_string(smonth)+"-"+to_string(sday)+" "+to_string(shour)+":"+to_string(sminute);
    else if(m==3) StartTime=to_string(syear)+"-"+to_string(smonth)+"-"+to_string(sday)+" "+to_string(shour);
    else if(m==2) StartTime=to_string(syear)+"-"+to_string(smonth)+"-"+to_string(sday);
    else if(m==1) StartTime=to_string(syear)+"-"+to_string(smonth);

}

QString Draw::getStartTime(void){
    //setStartTime(m_mode);
    //QString s=QString::fromStdString(StartTime);
    QString s=QString::fromStdString(StartTime);
    //qDebug()<<s;
    return s;
}

void Draw::co(bool co_){
    draw_co=co_;
}

void Draw::nox(bool nox_){
    draw_nox=nox_;
}

void Draw::o2(bool o2_){
    draw_o2=o2_;
}

void Draw::particulates(bool particulates_){
    draw_particulates=particulates_;
}

void Draw::so2(bool so2_){
    draw_so2=so2_;
}

void Draw::setWindowWidth(int width){
    window_width=width;
}

void Draw::setWindowHeight(int height){
    window_height=height;
}

void Draw::setMaxValue(int value){
    max_value=value;
}

QVector<QPointF> Draw :: findData(string gasname,int mode){
    QVector<QPointF> a;
    Dbutil db;
    QSqlDatabase data= db.connect();
    QSqlQuery query_id;
    QSqlQuery query_data;
    string std_time=StartTime;
    string sql_id;
    string tablename;
    if(mode==4) tablename="minute";
    else if(mode==3) tablename="hour";
    else if(mode==2) tablename="day";
    else if(mode==1) tablename="month";
    sql_id="select id from "+gasname+"_"+tablename+" where time = '"+std_time+"'";
    const char *k_id = sql_id.c_str();
    int id;
    bool success_id=query_id.exec(k_id);
    if(success_id) {
        while(query_id.next()) id=query_id.value(0).toInt();
    }else{
        qDebug()<<"数据不存在";
        return a;
    }
    string sql_data;
    sql_data="select concentration from "+gasname+"_"+tablename+" where id <="+to_string(id)+" and id >"+to_string(id-TimeRange);
    const char *k_data = sql_data.c_str();
    bool success_data = query_data.exec(k_data);
    if(success_data)  {
        int stepcount=0;
        int x_start=1;
        int step=window_width/(TimeRange-1);
        while(query_data.next()) {
            QPointF t;
            int coordinate_x=x_start+stepcount*step;
            if(coordinate_x>window_width) coordinate_x=window_width;
            t.setX(coordinate_x);
            int coordinate_y;
            int gasdata=query_data.value(0).toInt();
            coordinate_y=window_height-(gasdata*window_height)/max_value;
            t.setY(coordinate_y);
            a.push_back(t);
            stepcount++;
        }
    }
    db.dbclose(data);
    return a;
}

void Draw::paint(QPainter *painter){
    setStartTime(m_mode);
    if(draw_co){
        QVector<QPointF> data;
        data=findData("co",m_mode);
        co_painter=painter;
        co_painter->setRenderHint(QPainter::Antialiasing);
        QPen pen;
        pen.setColor("#FF79BC");
        co_painter->setPen(pen);
        for(int i=0;i<data.size()-1;i++){
            co_painter->drawLine(data[i],data[i+1]);
        }
    }
    if(draw_nox){
        QVector<QPointF> data;
        data=findData("nox",m_mode);
        nox_painter=painter;
        nox_painter->setRenderHint(QPainter::Antialiasing);
        QPen pen;
        pen.setColor("#2894FF");
        nox_painter->setPen(pen);
        for(int i=0;i<data.size()-1;i++){
            nox_painter->drawLine(data[i],data[i+1]);
        }
    }
    if(draw_o2){
        QVector<QPointF> data;
        data=findData("o2",m_mode);
        o2_painter=painter;
        o2_painter->setRenderHint(QPainter::Antialiasing);
        QPen pen;
        pen.setColor("#FFFF6F");
        o2_painter->setPen(pen);
        for(int i=0;i<data.size()-1;i++){
            o2_painter->drawLine(data[i],data[i+1]);
        }
    }
    if(draw_particulates){
        QVector<QPointF> data;
        data=findData("particulates",m_mode);
        particulates_painter=painter;
        particulates_painter->setRenderHint(QPainter::Antialiasing);
        QPen pen;
        pen.setColor("#1AFD9C");
        particulates_painter->setPen(pen);
        for(int i=0;i<data.size()-1;i++){
            particulates_painter->drawLine(data[i],data[i+1]);
        }
    }
    if(draw_so2){
        QVector<QPointF> data;
        data=findData("so2",m_mode);
        so2_painter=painter;
        so2_painter->setRenderHint(QPainter::Antialiasing);
        QPen pen;
        pen.setColor(Qt::red);
        so2_painter->setPen(pen);
        for(int i=0;i<data.size()-1;i++){
            so2_painter->drawLine(data[i],data[i+1]);
        }
    }
}



